using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.SqlSugarCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Volo.Abp.Domain.Entities;

namespace Volo.Abp.SqlSugarCore.DependencyInjection
{
    public class SqlSugarCoreRepositoryRegistrar : RepositoryRegistrarBase
    {
        public Assembly LoadAssembly { get; set; }
        public SqlSugarCoreRepositoryRegistrar(IServiceCollection services, Assembly assem) : base(services)
        {
            LoadAssembly = assem;
        }
        protected override IEnumerable<Type> GetEntityTypes()
        {
            return LoadAssembly.GetTypes().Where(e => e.IsAbstract == false && e.IsClass == true && typeof(IEntity).IsAssignableFrom(e));
        }

        protected override Type GetRepositoryType(Type entityType)
        {
            return typeof(SqlSugarCoreRepository<>).MakeGenericType(entityType);
        }

        protected override Type GetRepositoryType(Type entityType, Type primaryKeyType)
        {
            return typeof(SqlSugarCoreRepository<,>).MakeGenericType(entityType, primaryKeyType);
        }
    }
}